package com.qingcheng.consumer;

import com.qingcheng.dao.OrderMapper;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WeixinPayService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 未支付订单，关闭
 */
public class OrderTimeoutConsumer implements MessageListener {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WeixinPayService weixinPayService;

    @Override
    public void onMessage(Message message) {

        //提起订单号
        String orderId = new String(message.getBody());

        //查询业务系统的订单状态
        Order order = orderService.findById(orderId);
        if(order.getPayStatus().equals("0")){ //如果订单状态为未支付
            boolean flag = weixinPayService.checkOrder(orderId);
//            System.out.println("是否支付成功:"+flag);
            if(flag==false){ //订单未支付
                orderService.closeOrder(orderId);
            }else{
                //微信显示订单已支付，已此为准，进行补偿操作:修改状态，增加日志
                orderService.updateOrderForCompensate(orderId);
            }
        }else{

        }

    }
}
